package com.wangtao.firstapp.fragments.homepagerfragmnets;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.adapters.RVSifeAdapter;
import com.wangtao.firstapp.modles.SifeData;
import com.wangtao.firstapp.utils.HttpUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 精选页面
 * Created by lanouhn on 16/8/24.
 */
public class SifeFragment extends Fragment {

    private XRecyclerView srecyclerView;//创建recyclerView

    private List<SifeData.DataBean.TopicBean> sifeData = new ArrayList<>();//最终显示
    private List<SifeData.DataBean.TopicBean> datas = new ArrayList<>();//中间值

    int page = 0;

    private RVSifeAdapter srVSifeAdapter;//创建适配器


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_sift_listview, container, false);
        srecyclerView = (XRecyclerView) view.findViewById(R.id.recyclerView_sift_listView);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        srecyclerView.setLayoutManager(layoutManager);


        //上拉加载的动画效果
        srecyclerView.setRefreshProgressStyle(ProgressStyle.LineScalePulseOutRapid);
        //recyclerView.setLaodingMoreProgressStyle(ProgressStyle.Pacman);
        //下拉刷新的动画效果
        srecyclerView.setArrowImageView(R.drawable.ic_loading_rotate);

        srecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            //下拉刷新
            @Override
            public void onRefresh() {
                page = 0;
                getDataFromWeb();
            }

            //上拉加载
            @Override
            public void onLoadMore() {
                page++;
                getDataFromWeb();
            }
        });

        getDataFromWeb();
        return view;


    }

    private void getDataFromWeb() {
        try {
            String url = "http://open4.bantangapp.com/recommend/index";
            String params = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2560255&oauth_token=4df22230db195bba652ef0e593881122&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&page=" + page + "&pagesize=20&app_open_count=16&update_time=0&last_get_time=1471941779";
            HttpUtils.doPostAsyn(url, params, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson_sife = new Gson();
                    datas = gson_sife.fromJson(result, SifeData.class).getData().getTopic();
                    if (datas.size() > 0) {
                        handler.sendEmptyMessage(1);
                    } else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Toast.makeText(getActivity(), "没有数据", Toast.LENGTH_SHORT).show();
                    break;
                case 1:

                    if (page == 0) {//
                        //
                        sifeData.clear();
                        //
                        sifeData.addAll(datas);

                        srVSifeAdapter = new RVSifeAdapter(getActivity(), sifeData);
                        srecyclerView.setAdapter(srVSifeAdapter);

                        //srecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
                        //
                        srecyclerView.refreshComplete();
                        datas.clear();
                    } else {
                        sifeData.addAll(datas);
                        srVSifeAdapter.notifyDataSetChanged();
                        srecyclerView.loadMoreComplete();
                        datas.clear();
                    }

//                    srVSifeAdapter = new RVSifeAdapter(getActivity(), sifeData);
//                    srecyclerView.setAdapter(srVSifeAdapter);
//                    srecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
//                    //srVSifeAdapter.setOnSifeItemClickListener(SifeFragment.this);
                    break;
            }
        }
    };

//    @Override
//    public void onSifeItemClick(int pos) {
//        String item_ID = sifeData.getData().getTopic().get(pos).getId();
//
//        //文章，图，卖东西
//        String urlSecond = "http://open4.bantangapp.com/topic/newInfo";
//        String paramsSecond = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2573914&oauth_token=12977829338e5f953e3d24bcef502b6a&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&id=" + item_ID + "&trace_id=&relate_pid=&statistics_uv=1&is_night=0";
//
//        //大家都在看
//        String urlLook="http://open4.bantangapp.com/topics/topic/relationRec";
//        String paramsLook="app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2573914&oauth_token=12977829338e5f953e3d24bcef502b6a&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&id="+item_ID;
//
//        //评论
//        String urlReview="http://open4.bantangapp.com/topics/comment/list";
//        String paramsReview="app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2573914&oauth_token=12977829338e5f953e3d24bcef502b6a&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&page=0&pagesize=20&id="+item_ID;
//
//        //原创文章,有图
//        if (sifeData.getData().getTopic().get(pos).getUser().getIs_official() == 0) {
//
//            Intent intent = new Intent(getActivity(), OneSifeSecondActivity.class);
//
//            //文章，图，卖东西
//            intent.putExtra("urlSecond", urlSecond);
//            intent.putExtra("paramsSecond",paramsSecond);
//
//            //大家都在看
//            intent.putExtra("urlLook",urlLook);
//            intent.putExtra("paramsLook",paramsLook);
//
//            //评论
//            intent.putExtra("urlReview",urlReview);
//            intent.putExtra("paramsReview",paramsReview);
//
//            startActivity(intent);
//        }
//
//        //小糖君，有图
//        if (sifeData.getData().getTopic().get(pos).getUser().getIs_official() == 1 && sifeData.getData().getTopic().get(pos).getType().equals("")) {
//            Intent intent = new Intent(getActivity(), TwoSifeSecondActivity.class);
//            //文章，图，卖东西
//            intent.putExtra("urlSecond", urlSecond);
//            intent.putExtra("paramsSecond",paramsSecond);
//
//            //大家都在看
//            intent.putExtra("urlLook",urlLook);
//            intent.putExtra("paramsLook",paramsLook);
//
//            //评论
//            intent.putExtra("urlReview",urlReview);
//            intent.putExtra("paramsReview",paramsReview);
//
//            startActivity(intent);
//        }
//        //小唐君，无图
//        if (sifeData.getData().getTopic().get(pos).getUser().getIs_official() == 1 && !sifeData.getData().getTopic().get(pos).getType().equals("")) {
//            Intent intent = new Intent(getActivity(), ThreeSifeSecondActivity.class);
//            //文章，图，卖东西
//            intent.putExtra("urlSecond", urlSecond);
//            intent.putExtra("paramsSecond",paramsSecond);
//
//            //评论
//            intent.putExtra("urlReview",urlReview);
//            intent.putExtra("paramsReview",paramsReview);
//
//            startActivity(intent);
//        }
//

}
//}
